let express = require('express')
let router = express.Router()
let sqlQuery = require('../lcSql')

//中间件 (检查是否已经登录，若没有登录则无权访问详情页)
function isLogin(req, res, next) {
    console.log(req.session.username);
    if (!req.session.username) {
        res.redirect('/login')
    } else {
        next()
    }
}
router.get('/info/:bookId', async (req, res) => {
    let sqlStr = "select * from book where id=?"
    let bookId = req.params.bookId
    let result = await sqlQuery(sqlStr, bookId)
    let sqlStr2 = "select * from category"
    let result2 = await sqlQuery(sqlStr2)
    let options = {
        cid: 0,
        book: result[0],
        cataorys: Array.from(result2),
        isLoggedIn: req.session.isLoggedIn || false,
        username: req.session.username || ''
    }

    res.render('bookinfo', options)
})

module.exports = router